﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Molia.Premiere
{
    /// <summary>
    /// Setting.xaml 的交互逻辑
    /// </summary>
    public partial class Setting : Window
    {
        public Setting()
        {
            InitializeComponent();
            Static.GlobalField.ThemeProvider.Registry(this);
        }

        Button _button;
        object _brush;
        private void Select_Color_Background(object sender, SelectionChangedEventArgs e)
        {
            var comb = sender as ComboBox;
            if (comb != null)
            {
                var item = comb.Items[comb.SelectedIndex];
                if (item.GetType() == typeof(TextBlock))
                {
                    var opendlg = new OpenFileDialog()
                    {
                        Filter = "(*.jpg,*.png,*.jpeg,*.bmp,*.gif)|*.jgp;*.png;*.jpeg;*.bmp;*.gif|All files(*.*)|*.*"
                    };
                    if (opendlg.ShowDialog() == true)
                    {
                        var aq = new Image()
                        {
                            Stretch = Stretch.Uniform,
                            Width=32,
                            Height=32
                        };
                        aq.Source = new BitmapImage(new Uri(opendlg.FileName));
                        var i = comb.Items.Add(aq);
                        comb.SelectedIndex=i;
                        comb.Items.Remove(item);
                        comb.Items.Add(item);
                        Static.GlobalField.ThemeProvider["Theme.Background"] = new ImageBrush(aq.Source);
                    }
                }
                else if (item.GetType() == typeof(Rectangle))
                {
                    comb.SelectedItem = item;
                    Static.GlobalField.ThemeProvider["Theme.Background"] = (item as Rectangle).Fill;
                }
                else if (item.GetType() == typeof(Image))
                {
                    Static.GlobalField.ThemeProvider["Theme.Background"] = new ImageBrush((item as Image).Source);
                }
            }
        }
        private void Select_Color_Foreground(object sender,RoutedEventArgs e)
        {
            _button = sender as Button;
            if(_button!= null)
            {
                Select_Color_Slider.IsOpen = true;
                _brush = _button.Tag;
            }
        }
        private void Select_Color_Foreground_Changed(object sendr,RoutedPropertyChangedEventArgs<double> e)
        {
            _button.Background = Color_Preview.Fill;
           Static.GlobalField.ThemeProvider[_brush.ToString()] = Color_Preview.Fill;    
        }
    }
}
